<?php
class Controller_Admin_SuperData extends  Controller_Admin_Template 
{
        public function __construct($req, $res) 
        {
		    $this->template = "smarty:superdata/daily";
		    parent::__construct($req, $res);
	    }

        public function action_index ()
        {
            $this->request->redirect('/superdata/daily');
        }

        public function action_daily($uid = NULL, $start_date = NULL, $end_date = NULL)
        {
            $limitday = $_GET['limitday'];
            $start_date = $_GET['start_date'];
            $end_date   = $_GET['end_date'];
            $machine   = $_GET['machine'];
            $number   = $_GET['number'];
            if($uid==NULL){
                $uid   = $_GET['uid'];
            }else{
                $tmp =  SuperData::machines($uid);
                $machine = $tmp['0']['realname'];
            }

            if($machine==''){
                $machine='all';
            }

            if($limitday == NULL){
                $end_date  = empty($end_date)?date('Y/m/d', time()):date('Y/m/d', strtotime($end_date));
                $start_date = empty($start_date)?date('Y/m/d', time()-86400*7):date('Y/m/d', strtotime($start_date));
            }  else {
                $end_date  = empty($end_date)?date('Y/m/d', time()):date('Y/m/d', strtotime($end_date));
                $start_date = empty($start_date)?date('Y/m/d', time()-86400*$limitday+86400):date('Y/m/d', strtotime($start_date));
            }
            
            $machines = SuperData::machines(); 
            $daily = SuperData::stats($uid,null,$start_date,$end_date,$machine,$number);
            $line  = SuperData::build_line($daily,$start_date,$end_date);
            $this->template->set('line', $line);
            $this->template->set('start_date', $start_date);
            $this->template->set('end_date', $end_date);
            $this->template->set('machines', $machines);
            $this->template->set('machine_selected', $machine);
            $this->template->set('number', $number);
            $this->template->set('daily', $daily);
            $this->template->set('months', tools_date::between_months('20140801'));
        }
        
        public function action_apps($imei, $day, $machine)
        {
            $apps = SuperData::apps($imei, $day, $machine);
            $this->template->set('apps', $apps);
            $this->template->set_filename('smarty:superdata/apps');
        }

        public function action_dianmian($uid)
        {
            $daily = SuperData::stats($uid);
            $this->template->set('daily', $daily);
        }

        public function action_day ($day, $uid)
        {
            $records = SuperData::daily($day, $uid);
            $this->template->set('records', $records);
            $this->template->set_filename('smarty:superdata/day');
        }

        public function action_verify_logs ($uid)
        {
            $post = $this->request->post();
            $pids = Arr::get($post, 'p');
            if (empty($pids) || !is_array($pids))
            {
                throw new Exception("非法数据");
            }

            $refer = $this->request->referrer();
            SuperData::verify_logs($uid, $pids);
            $this->request->redirect($refer);
        }


    public function action_cppa ($days = NULL)
    {
        $start_day = $_GET['start_date'];
        $stop_day   = $_GET['end_date'];

        if($days == NULL){
            $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
            $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
        }  else {
            $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
            $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
        }
        
       
        $daily = CPPA_Cppa::stats($start_day, $stop_day);
        Session::instance()->set('result_cppa', $daily); 
//        var_dump($daily);exit;
        
        $this->template->set('daily', $daily);
        $this->template->set_filename('smarty:admin/cppa/daily');
        $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
        $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
    }
    
        public function action_cppa_apps($imei, $day, $uid)
        {
            $apps = CPPA_Cppa::apps($imei, $day, $uid);
            $dianming = CPPA_Cppa::dianming($uid);
            $this->template->set('apps', $apps);
            $this->template->set('dianming', $dianming);
            $this->template->set_filename('smarty:admin/cppa/apps');
        }

        public function action_cppa_daily($day, $uid)
        {
            $records = CPPA_Cppa::daily($day, $uid);
            $this->template->set('records', $records);
            $this->template->set_filename('smarty:admin/cppa/day');
        }
        
        public function action_cppa_active ($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::active($start_day, $stop_day);
            Session::instance()->set('cppa_active', $daily); 
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/active');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }

        public function action_cppa_apks ($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::arrival_apks($start_day, $stop_day);
//            var_dump($daily['apks']);exit;
            Session::instance()->set('result_cppa', $daily); 
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/apks');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        //
        //渠道每日应用活跃
        //
        public function action_cppa_apks_active ($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::active_apks($start_day, $stop_day);
//            var_dump($daily['list']);exit;
//            Session::instance()->set('result_cppa', $daily); 
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/apks_active');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        public function action_active_apks_detail($channel = NULL,$day = NULL)
        {
            $result = cppa_cppa::active_apks_detail($channel, $day);
            $passport = cppa_cppa::getPassport($channel);
//            var_dump($result);exit;
            $this->template->set('items', $result);
            $this->template->set('passport', $passport);
            $this->template->set('day', $day);
            $this->template->set_filename('smarty:admin/cppa/active_apks_detail');
        }
        
        public function action_arrival_apks_detail($packagename = NULL,$day = NULL)
        {
            $result = cppa_cppa::arrival_apks_detail($packagename, $day);
//            var_dump($result);exit;
            $this->template->set('items', $result);
            $this->template->set_filename('smarty:admin/cppa/apks_detail');
        }

        private function show_upload()
        {
            $this->template->set('records', SuperData::sdlogs(user::current_user_id()));
            $this->template->set_filename('smarty:superdata/upload');
        }

        //
        // 导出所有上报的日志数据到excel里
        //
        public function action_excel ($month)
        {
            ini_set('max_execution_time', 300);
            ini_set('memory_limit', '1024M');

            $result = superdata::daily(NULL, NULL, $month);
            $title  = array(
                '日期', 
                '品牌',
                '型号',
                'IMEI',
                '应用数',
                '网点',
                '编号',
                '状态',
            );

            $data = array();
            $index = 0;
            foreach ($result['records'] as & $rs)
            {
                $data[] = array(
                    $rs['day'],
                    $rs['brand'],
                    $rs['types'],
                    $rs['IMEI'],
                    $rs['apps'],
                    $rs['dianmian'],
                    $rs['machine'],
                    $rs['is_public'] == 1 ? '通过' : '-',
                );
            }

            tools_excel::export2($data, $title, "$month-CPZ.xls"); 
        }
    
        public function action_show_apks($days = NULL)
        {
            $start_day = $_GET['start_date'];
            $stop_day   = $_GET['end_date'];
//            var_dump($days);exit;
            if($days == NULL){
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  else {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
            $result = superdata::show_apks($start_day, $stop_day);
            Session::instance()->set('result_apks', $result); 
//            var_dump($result);exit;
            $this->template->set('items', $result);
            $this->template->set_filename('smarty:superdata/show_apks');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
         public function action_excel_apks()
         {
            ini_set('max_execution_time', 300);
            ini_set('memory_limit', '1024M');
            
            $result  =  Session::instance()->get('result_apks');
                        
            $title = array();
            $title[] = 'apk';
            foreach ($result['days'] as $key => $value) {
                $title[] = $value;
            }
            $title[] = '日均';
            $title[] = '总计';
//            var_dump($title);exit;
            
            $data = array();
            foreach ($result['list'] as  $key => $rs)
            {
                $data1 = array();
                $data1[] = $key;
                foreach ($rs as $key1 => $value) {
                    $data1[] = $value;
                }
                $data[] = $data1;
            }
           
            $data2 = array();
            $data2[] = '合计';
            foreach ($result['count'] as $key => $value) {
                $data2[] = $value;
            }
            $data[] = $data2;
//            var_dump($data);exit;
            
            tools_excel::export2($data, $title, 'apksnumber'); 
         }
         
          public function action_excel_cppa()
          {
                ini_set('max_execution_time', 300);
                ini_set('memory_limit', '1024M');
                
                $result = Session::instance()->get('result_cppa');
//                var_dump($result['list']);exit;
                //构造标题
                $title = array();
                $title[] = '渠道名称';
                foreach ($result['days'] as $key => $value) {
                    $title[] = $value;
                }
                $title[] = '日均';
                $title[] = '总计';
   
                //构造数据主体
                $data = array();
                foreach ($result['list'] as  $key => $rs)
                {
                    $data1 = array();
                    $data1[] = empty($key)?'---':$key;
                    foreach ($rs as $key1 => $value) {
                        if($key1 == 'sum'||$key1 == 'average')   continue;
                        $data1[] = empty($value['imei'])?0:$value['imei'];
                    }
                    $data1[] = $rs['average'];
                    $data1[] = $rs['sum'];
                    $data[] = $data1;
                }
                
                $data2 = array();
                $data2[] = '合计';
                foreach ($result['sum'] as $key => $value) {
                    $data2[] = $value;
                }
                $data[] = $data2;
                
//                var_dump($data);exit;
                tools_excel::export2($data, $title, 'cppa'); 
          }

          public function action_excel_cppa_apks()
          {
                ini_set('max_execution_time', 300);
                ini_set('memory_limit', '1024M');
                
                $result = Session::instance()->get('result_cppa');
//                var_dump($result['list']);exit;
                //构造标题
                $title = array();
                $title[] = '应用名称';
                foreach ($result['days'] as $day => $n) {
                    $title[] = $day;
                }
                $title[] = '日均';
                $title[] = '总计';
   
                //构造数据主体
                $data = array();
                foreach ($result['apks'] as  $pname => $pval)
                {
                    $data1 = array();
                    $data1[] = $pval['appname'];
                    foreach ($pval['days'] as $day => $n) 
                        $data1[] = $n;
                    $data1[] = $pval['avg'];
                    $data1[] = $pval['sum'];
                    $data[] = $data1;
                }
                
                $data2 = array();
                $data2[] = '合计';
                foreach ($result['sums'] as $day => $n) {
                    $data2[] = $n;
                }
                $data2[] = $result['sum_avg'];
                $data2[] = $result['sum_total'];
                $data[] = $data2;
                
//                var_dump($data);exit;
                tools_excel::export2($data, $title, 'CPPA-APKS'); 
          }
          
          public function action_excel_active()
          {
                ini_set('max_execution_time', 300);
                ini_set('memory_limit', '1024M');
                
                $result = Session::instance()->get('cppa_active');
//                var_dump($result['list']);exit;
                //构造标题
                $title = array();
                $title[] = '渠道名称';
                foreach ($result['days'] as $key => $value) {
                    $title[] = $value;
                }
                $title[] = '日均';
                $title[] = '总计';
   
                //构造数据主体
                $data = array();
                foreach ($result['list'] as  $key => $rs)
                {
                    $data1 = array();
                    $data1[] = empty($key)?'---':$key;
                    foreach ($rs as $key1 => $value) {
                        if($key1 == 'sum'||$key1 == 'average')   continue;
                        $data1[] = empty($value)?0:$value;
                    }
                    $data1[] = $rs['average'];
                    $data1[] = $rs['sum'];
                    $data[] = $data1;
                }
                
                $data2 = array();
                $data2[] = '合计';
                foreach ($result['sum'] as $key => $value) {
                    $data2[] = $value;
                }
                $data[] = $data2;
                
//                var_dump($title);var_dump($data);exit;
                tools_excel::export2($data, $title, 'active'); 
          }
          
        //
        //友盟渠道每日新增
        //
        public function action_umeng_active($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::umeng_active($start_day, $stop_day);
//            var_dump($daily['items']);exit;
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/umeng_active');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        //
        //友盟渠道每日新增
        //
        public function action_umeng_newbies($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::umeng_newbies($start_day, $stop_day);
//            var_dump($daily['items']);exit;
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/umeng_newbies');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        //
        //友盟渠道每日启动次数
        //
        public function action_umeng_launch($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::umeng_launch($start_day, $stop_day);
//            var_dump($daily['items']);exit;
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/umeng_launch');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        //
        //友盟渠道每日使用时长
        //
        public function action_umeng_duration($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::umeng_duration($start_day, $stop_day);
//            var_dump($daily['items']);exit;
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/umeng_duration');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        //
        //友盟渠道每日最新总计
        //
        public function action_umeng_total_install($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::umeng_total_install($start_day, $stop_day);
//            var_dump($daily['items']);exit;
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/umeng_total_install');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        //
        //友盟渠道每日最新总计比例
        //
        public function action_umeng_total_install_rate($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*15):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::umeng_total_install_rate($start_day, $stop_day);
//            var_dump($daily['items']);exit;
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/umeng_total_install_rate');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        /**
         * SDK广告上报数据展示
         */
        public function action_cppa_show_log ($days = NULL)
        {
            $start_day = $this->request->query('start_date');
            $stop_day  = $this->request->query('end_date');

            if (empty($days))
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*5):date('Ymd', strtotime($start_day));
            }  
            else 
            {
                $stop_day  = empty($stop_day)?date('Ymd', time()):date('Ymd', strtotime($stop_day));
                $start_day = empty($start_day)?date('Ymd', time()-86400*$days+86400):date('Ymd', strtotime($start_day));
            }
       
            $daily = cppa_cppa::show_ad_log($start_day, $stop_day);
//            Session::instance()->set('cppa_active', $daily); 
//            var_dump($daily['list']);exit;
        
            $this->template->set('daily', $daily);
            $this->template->set_filename('smarty:admin/cppa/ad_log');
            $this->template->set('start_date', date('Y/m/d', strtotime($start_day)));
            $this->template->set('end_date', date('Y/m/d', strtotime($stop_day)));
        }
        
        public function action_cppa_ad_log_detail($type, $day)
        {
            $items = cppa_cppa::show_ad_log_dtail($type, $day);
            $this->template->set('items', $items);
            $this->template->set_filename('smarty:admin/cppa/ad_log_detail');
        }
    
}
